<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型编码" prop="typeCode">
        <el-input
          v-model="queryParams.typeCode"
          placeholder="请输入类型编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型名称" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入类型名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['mbti:personalityType:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mbti:personalityType:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mbti:personalityType:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['mbti:personalityType:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="personalityTypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类型编码" align="center" prop="typeCode" width="100" />
      <el-table-column label="类型名称" align="center" prop="typeName" width="150" />
      <el-table-column label="类型昵称" align="center" prop="typeNickname" width="150" />
      <el-table-column label="人群占比" align="center" prop="percentage" width="100">
        <template #default="scope">
          <span v-if="scope.row.percentage">{{ scope.row.percentage }}%</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="详细描述" align="center" prop="description" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['mbti:personalityType:query']">详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['mbti:personalityType:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['mbti:personalityType:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改MBTI人格类型对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="personalityTypeRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型编码" prop="typeCode">
              <el-input v-model="form.typeCode" placeholder="请输入类型编码" maxlength="10" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型名称" prop="typeName">
              <el-input v-model="form.typeName" placeholder="请输入类型名称" maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型昵称" prop="typeNickname">
              <el-input v-model="form.typeNickname" placeholder="请输入类型昵称" maxlength="100" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="人群占比" prop="percentage">
              <el-input-number v-model="form.percentage" :min="0" :max="100" :precision="2" placeholder="请输入人群占比" />
              <span style="margin-left: 5px;">%</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="详细描述" prop="description">
          <el-input v-model="form.description" type="textarea" :rows="3" placeholder="请输入详细描述" />
        </el-form-item>
        <el-form-item label="优势特点" prop="strengths">
          <el-input v-model="form.strengths" type="textarea" :rows="3" placeholder="请输入优势特点" />
        </el-form-item>
        <el-form-item label="劣势特点" prop="weaknesses">
          <el-input v-model="form.weaknesses" type="textarea" :rows="3" placeholder="请输入劣势特点" />
        </el-form-item>
        <el-form-item label="职业建议" prop="careerSuggestions">
          <el-input v-model="form.careerSuggestions" type="textarea" :rows="3" placeholder="请输入职业建议" />
        </el-form-item>
        <el-form-item label="人际关系建议" prop="relationshipTips">
          <el-input v-model="form.relationshipTips" type="textarea" :rows="3" placeholder="请输入人际关系建议" />
        </el-form-item>
        <el-form-item label="著名人物" prop="famousPeople">
          <el-input v-model="form.famousPeople" type="textarea" :rows="2" placeholder="请输入著名人物" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog title="人格类型详情" v-model="viewOpen" width="900px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="类型编码">{{ viewForm.typeCode }}</el-descriptions-item>
        <el-descriptions-item label="类型名称">{{ viewForm.typeName }}</el-descriptions-item>
        <el-descriptions-item label="类型昵称">{{ viewForm.typeNickname }}</el-descriptions-item>
        <el-descriptions-item label="人群占比">{{ viewForm.percentage }}%</el-descriptions-item>
        <el-descriptions-item label="详细描述" :span="2">{{ viewForm.description }}</el-descriptions-item>
        <el-descriptions-item label="优势特点" :span="2">{{ viewForm.strengths }}</el-descriptions-item>
        <el-descriptions-item label="劣势特点" :span="2">{{ viewForm.weaknesses }}</el-descriptions-item>
        <el-descriptions-item label="职业建议" :span="2">{{ viewForm.careerSuggestions }}</el-descriptions-item>
        <el-descriptions-item label="人际关系建议" :span="2">{{ viewForm.relationshipTips }}</el-descriptions-item>
        <el-descriptions-item label="著名人物" :span="2">{{ viewForm.famousPeople }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup name="PersonalityType">
import { listPersonalityType, getPersonalityType, delPersonalityType, addPersonalityType, updatePersonalityType, checkTypeCodeUnique } from "@/api/mbti/personalityType";

const { proxy } = getCurrentInstance();
const { parseTime } = proxy.useDict();

const personalityTypeList = ref([]);
const open = ref(false);
const viewOpen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  viewForm: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    typeCode: null,
    typeName: null,
  },
  rules: {
    typeCode: [
      { required: true, message: "类型编码不能为空", trigger: "blur" },
      { min: 4, max: 4, message: "类型编码必须为4个字符", trigger: "blur" }
    ],
    typeName: [
      { required: true, message: "类型名称不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, viewForm, rules } = toRefs(data);

/** 查询MBTI人格类型列表 */
function getList() {
  loading.value = true;
  listPersonalityType(queryParams.value).then(response => {
    personalityTypeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    typeId: null,
    typeCode: null,
    typeName: null,
    typeNickname: null,
    description: null,
    strengths: null,
    weaknesses: null,
    careerSuggestions: null,
    relationshipTips: null,
    famousPeople: null,
    percentage: null
  };
  proxy.resetForm("personalityTypeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.typeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加MBTI人格类型";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _typeId = row.typeId || ids.value
  getPersonalityType(_typeId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改MBTI人格类型";
  });
}

/** 查看详情 */
function handleView(row) {
  getPersonalityType(row.typeId).then(response => {
    viewForm.value = response.data;
    viewOpen.value = true;
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["personalityTypeRef"].validate(valid => {
    if (valid) {
      if (form.value.typeId != null) {
        updatePersonalityType(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPersonalityType(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _typeIds = row.typeId || ids.value;
  proxy.$modal.confirm('是否确认删除MBTI人格类型编号为"' + _typeIds + '"的数据项？').then(function() {
    return delPersonalityType(_typeIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('mbti/personalityType/export', {
    ...queryParams.value
  }, `personalityType_${new Date().getTime()}.xlsx`)
}

getList();
</script>
